﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Setting_SelectRegionViewModel : CCommandViewModel
    {
        List<cRegion> _AvailableRegions = null;
        string _SelectedRegion = "-1";

        public Setting_SelectRegionViewModel() 
        {
            base.ViewName = LanguageStrings.GetText("Select a Region");
            InitRegions();
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Discard"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
        }

        public Setting_SelectRegionViewModel(List<cRegion> AvailableRegions) 
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("Select a Region");
                InitRegions();

                _AvailableRegions.AddRange(AvailableRegions);
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Discard"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            }
            catch (Exception Ex)
            {

            }
        }

        #region Properties

        /// <summary>
        /// Contains the current in Combo-box selected Region
        /// </summary>
        public string SelectedRegion
        {
            get { return _SelectedRegion; }
            set { _SelectedRegion = value; }
        }

        /// <summary>
        /// Contains a list with all configured Regions
        /// </summary>
        public List<cRegion> Data
        {
            get { return _AvailableRegions; }
        }

        public CSingleCommandViewModel Cmd_Apply
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_Discard
        {
            get { return m_oCommands[1]; }
        }

        #endregion

        #region Command Procedures

        void Cmd_ApplyChanges()
        {
            try
            {
                Settings_SelectRegion ParentWindow = null;
                if ((Settings_SelectRegion)((CDelegatingCommand)Cmd_Apply.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_SelectRegion)((CDelegatingCommand)Cmd_Apply.Command).CommandParameter;

                    if (_SelectedRegion != "-1")
                    {
                        if (ParentWindow != null)
                        {
                            ParentWindow.DialogResult = true;
                            ParentWindow.Close();
                        }
                    }
                    else
                    {
                        
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_APPLY_SETTINGS, Ex, EMessageTypes.Error);
            }
        }

        void CMd_DiscardChanges()
        {
            try
            {
                Settings_SelectRegion ParentWindow = null;
                if ((Settings_SelectRegion)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_SelectRegion)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter;

                    if (ParentWindow != null)
                        ParentWindow.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_DISCARD_SETTINGS, Ex, EMessageTypes.Error);
            }
        }

        #endregion

        void InitRegions()
        {
            try
            {
                _AvailableRegions = new List<cRegion>();
                cRegion tmpNewRegion = new cRegion();
                tmpNewRegion.Caption = LanguageStrings.GetText("Please choose a Region");
                tmpNewRegion.Name = "-1";
                _AvailableRegions.Add(tmpNewRegion);
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_INITIALIZE_SELECTED_AVAILABLE_REGIONS, Ex, EMessageTypes.Error);
            }
        }
    }
}
